package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Util;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

public class TreeHandler implements Listener {
    @EventHandler
    public void on(BlockBreakEvent e) {
        if (!e.isCancelled()) {
            if (e.getBlock().getType() == Material.ACACIA_LOG
                    || e.getBlock().getType() == Material.BIRCH_LOG
                    || e.getBlock().getType() == Material.DARK_OAK_LOG
                    || e.getBlock().getType() == Material.JUNGLE_LOG
                    || e.getBlock().getType() == Material.OAK_LOG
                    || e.getBlock().getType() == Material.SPRUCE_LOG) {
                ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
                ItemMeta itemMeta = item.getItemMeta();
                if (itemMeta == null) return;
                if (itemMeta.getDisplayName().equals(Util.getStr("LumberAxe"))) {
                    if (this.isAxe(e.getPlayer().getInventory().getItemInMainHand())) {
                        if (e.getPlayer().getGameMode().equals(GameMode.SURVIVAL))
                            this.breakBlock(e.getPlayer(), e.getBlock());
                    }
                }
            }
        }
    }

    private void breakBlock(Player p, Block b) {
        b.breakNaturally();
        Location above = new Location(b.getWorld(), b.getLocation().getBlockX(), (b.getLocation().getBlockY() + 1), b.getLocation().getBlockZ());
        Block blockAbove = above.getBlock();
        if (blockAbove.getType() == Material.ACACIA_LOG
                || blockAbove.getType() == Material.BIRCH_LOG
                || blockAbove.getType() == Material.DARK_OAK_LOG
                || blockAbove.getType() == Material.JUNGLE_LOG
                || blockAbove.getType() == Material.OAK_LOG
                || blockAbove.getType() == Material.SPRUCE_LOG) {
            this.breakBlock(p, blockAbove);
            ItemStack item = p.getInventory().getItemInMainHand();
            Damageable itemDamage = (Damageable) item.getItemMeta();
            if (itemDamage == null) return;
            itemDamage.setDamage(itemDamage.getDamage() + 1);
            if (itemDamage.getDamage() > item.getType().getMaxDurability()) {
                p.getInventory().remove(item);
                p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F);
            }
        }
    }

    private boolean isAxe(ItemStack item) {
        return item.getType().equals(Material.WOODEN_AXE)
                || item.getType().equals(Material.STONE_AXE)
                || item.getType().equals(Material.IRON_AXE)
                || item.getType().equals(Material.GOLDEN_AXE)
                || item.getType().equals(Material.DIAMOND_AXE);
    }
}
